package com.wanxi.servlet.lawyer;

import com.alibaba.fastjson.JSONObject;
import com.wanxi.dto.ResultDto;
import com.wanxi.model.LawyerModel;
import com.wanxi.service.Impl.LawyerServiceImpl;
import com.wanxi.service.LawyerService;
import com.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Li
 * @Date 2023/5/4 19:39
 */
@WebServlet("/back/AddLawyer")
public class LawyerAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //2.获取前端数据
        String name = req.getParameter("name");
        String image = req.getParameter("image");
        String age = req.getParameter("age");
        String sex = req.getParameter("sex");
        String role = req.getParameter("role");
        String phone = req.getParameter("phone");
        String introduce = req.getParameter("introduce");
        String enable = req.getParameter("enable");
        String remark = req.getParameter("remark");
        //3.封装model
        LawyerModel lawyerModel = LawyerModel.builder()
                .name(name)
                .image(image)
                .age(Tool.nullToInt(age))
                .sex(Tool.nullToStr(sex))
                .role(Tool.nullToStr(role))
                .phone(Tool.nullToStr(phone))
                .introduce(Tool.nullToStr(introduce))
                .enable(Tool.nullToInt(enable))
                .remark(Tool.nullToStr(remark))
                .build();
        //4.调用service层
        LawyerService lawyerService = new LawyerServiceImpl();
        ResultDto resultDto = lawyerService.add(lawyerModel);
        //5.返回前台数据
        resp.getWriter().println(JSONObject.toJSONString(resultDto));

    }
}
